$(document).ready(function() {
    $('#summernote').summernote({
        height: 300,
        tabsize: 2,
        lang: 'zh-CN',
        placeholder: '请输入问题的详细描述...',
        callbacks:{
            //summernote内部定义了一个特殊方法，会自动在用户选中图片后运行
            onImageUpload:function(files){
                let file = files[0]; //参数是个数组，实际上数组的第一个元素就是我们选中的图片

                let form = new FormData();
                form.append("imageFile",file);
                $.ajax({
                    url: "/resource/v1/images",
                    method: 'post',
                    data: form,
                    cache:false,//本次提交内容不缓存
                    //使用ajax执行文件上传必须设置contentType processData为false，否则会按照字符串数据处理上传；
                    contentType: false,
                    processData: false,
                    success:function(r){
                        console.log(r);
                        if(r.code == OK){
                            let img = new Image();
                            img.src = r.message;
                            //将设置好路径的img添加到富文本编辑器中
                            $('#summernote').summernote('insertNode',img)
                        }else {
                            alert(r.message)
                        }
                    }
                })
            }
        }

    });
    $('select').select2({placeholder:'请选择...'});
});

let postAnswerApp = new Vue({
    el: '#postAnswerApp',
    data: {
        message: '',
        hasError: false
    },
    methods:{
        postAnswer:function(){
            // 问题id
            this.hasError = false;
            //获取答案内容
            let questionId = location.search;
            if(!questionId){
                this.message = '没有问题ID'
                this.hasError = true;
                return;
            }
            questionId = questionId.substring(1);
            //教师回答内容
            let content = $('#summernote').val()
            if(!content){
                this.message = '必须填写回答内容'
                this.hasError = true;
                return;
            }
            //提交form表单
            let form = {
                questionId: questionId,
                content: content
            }
            //ajax
            $.ajax({
                url: '/faq/v1/answers',
                method: 'post',
                data: form,
                success: function(r){
                    if(r.code == CREATED){
                        //接收新增的answer对象
                        let answer = r.data;
                        //将answer对象追加到answers数组中
                        answersApp.answers.push(answer)
                        //清空summernote
                        $('#summernote').summernote('reset')
                        postAnswerApp.message = r.message
                        postAnswerApp.hasError = true;
                    } else {
                        postAnswerApp.message = r.message
                        postAnswerApp.hasError = true;
                    }
                }
            });


        }
    }
})